<?php

class tempRotinaExame extends Zend_Db_Table
{
    protected $_name = "_temp_rotina_exame";
    
    
     public function _getExame($id)
    {
        
        $select = $this->select()
                       ->distinct()
                       ->from($this, 'idtemp')
                       ->where(' idtemp = ?  ', $id);
        
        
        $result = $this->fetchRow($select);
        
          if ($result->idtemp == "")
            return true;
          else
            return false;
       
         
        
    }
    
    
    public function _existeExame($idExame, $idtemp)
    {
        $select = $this->select()
                       ->from($this,'count(*) as qtd')
                       ->where(' idExame = ?', $idExame)
                       ->where(' idtemp = ?  ', $idtemp);
                  
        
        $resultado = $this->fetchRow($select);
         if ($resultado->qtd > 0){
                    return true;
                }
                else
                {
                    return false;
                }
               
    }
    
     
   
    public function removeExame($idtemp,$idExame)
    {
        $retorno = true;
        $where= "";
        $where  =  $this->getAdapter()->quoteInto("  idExame = ? ", $idExame);
        $where  .= $this->getAdapter()->quoteInto("  and idtemp = ? ", $idtemp);
        
        try {
            $this->_db->beginTransaction();
            $this->delete($where);
            $this->_db->commit();
        } catch (Zend_Exception $exc) {
            print_r($exc);
            $this->_db->rollBack();
            $retorno = false;
        }

        return $retorno;
                  
        
               
    }
   
    public function edit($idtemp,$idExame,$idKitSorologia)
    {
        
        $retorno = true;
        $where= "";
        $where  =  $this->getAdapter()->quoteInto("  idExame = ? ", $idExame);
        $where  .= $this->getAdapter()->quoteInto("  and idtemp = ? ", $idtemp);
    
        $dados = array(
                'idKitSorologia' => $idKitSorologia
        );  

        try {
            $this->_db->beginTransaction();
            $this->update($dados, $where) ;
            $this->_db->commit();
        } catch (Zend_Exception $exc) {
            print_r($exc->getMessage());
            $this->_db->rollBack();
            $retorno = false;
        }

        return $retorno;
        
    }
    public function add($idtemp,$idExame)
    {
	$dados = array(
            		'idtemp' => $idtemp,
                        'dataRotina' => date('y-m-d'),
			'idExame'=> $idExame
	);  
          
         $this->_db->beginTransaction();
         try
         {
         
            $this->insert($dados); 
            $this->_db->commit();
            return true;
           
         }
         catch (Zend_Exception $e)
         {
            $this->_db->rollBack(); 
            print_r($e->getMessage());
            
            return false;
            
         }
         
       
	 
    }
    
    
    public function getExameTemp($idTemp)
    {
        $select = $this->select()
                        ->from($this)
                        ->where(' idtemp = ?', $idTemp);
        
         $resultado = $this->fetchAll($select);
         return $resultado;
    }

    public function delTempExame($idtemp)
    {
        $where = $this->_db->quoteInto(' idtemp = ?',$idtemp );
       
        $result = $this->delete($where);
        print_r($result);
        return $result;
    }
}
